package com.rickpan.dto.share;

import com.rickpan.entity.FileShare;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 更新分享设置请求DTO
 * 
 * @author RickPan Team
 * @since 2025-01-06
 */
public class UpdateShareRequest {

    /**
     * 分享类型
     */
    private FileShare.ShareType shareType;

    /**
     * 访问密码 (当shareType为PASSWORD时必填)
     */
    @Size(min = 4, max = 20, message = "密码长度必须在4-20位之间")
    private String password;

    /**
     * 允许下载
     */
    private Boolean allowDownload;

    /**
     * 允许预览
     */
    private Boolean allowPreview;

    /**
     * 过期时间
     */
    private LocalDateTime expireTime;

    /**
     * 最大访问次数
     */
    @Min(value = 1, message = "最大访问次数必须大于0")
    private Integer maxAccessCount;

    // 构造函数
    public UpdateShareRequest() {}

    // Getter 和 Setter 方法
    public FileShare.ShareType getShareType() { return shareType; }
    public void setShareType(FileShare.ShareType shareType) { this.shareType = shareType; }

    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }

    public Boolean getAllowDownload() { return allowDownload; }
    public void setAllowDownload(Boolean allowDownload) { this.allowDownload = allowDownload; }

    public Boolean getAllowPreview() { return allowPreview; }
    public void setAllowPreview(Boolean allowPreview) { this.allowPreview = allowPreview; }

    public LocalDateTime getExpireTime() { return expireTime; }
    public void setExpireTime(LocalDateTime expireTime) { this.expireTime = expireTime; }

    public Integer getMaxAccessCount() { return maxAccessCount; }
    public void setMaxAccessCount(Integer maxAccessCount) { this.maxAccessCount = maxAccessCount; }

    /**
     * 验证请求参数
     */
    public void validate() {
        if (shareType == FileShare.ShareType.PASSWORD) {
            if (password == null || password.trim().isEmpty()) {
                throw new IllegalArgumentException("密码分享必须设置访问密码");
            }
            if (password.length() < 4 || password.length() > 20) {
                throw new IllegalArgumentException("密码长度必须在4-20位之间");
            }
        }
    }

    @Override
    public String toString() {
        return "UpdateShareRequest{" +
                "shareType=" + shareType +
                ", password='" + (password != null ? "***" : null) + '\'' +
                ", allowDownload=" + allowDownload +
                ", allowPreview=" + allowPreview +
                ", expireTime=" + expireTime +
                ", maxAccessCount=" + maxAccessCount +
                '}';
    }
}
